<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="reg" label-width="0px" class="ms-content">
                <el-form-item prop="name">
                    <el-input v-model="param.name" placeholder="输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="idCard">
                    <el-input v-model="param.idCard" placeholder="输入身份证号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="param.password" type="password" placeholder="输入密码">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input v-model="param.phone" type="text" placeholder="输入手机号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="age">
                    <el-input v-model="param.age" placeholder="输入年龄">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="province">
                    <el-input v-model="param.province" placeholder="输入籍贯">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="college">
                    <el-select v-model="param.college" placeholder="请选择学院">
                        <el-option v-for="item in collegeList" :key="item.id" :label="item.name" :value="item.name" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="nation">
                    <el-input v-model="param.nation" placeholder="输入民族">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="registerForm()">注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            param: {
                name: '',
                idCard: '',
                age: '',
                province: '',
                college: '',
                nation: '',
                password: '',
                phone: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入名字名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                idCard: [{ required: true, message: '请输入18位身份证号', trigger: 'blur' }],
                age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
                province: [
                    { required: true, message: '请输入籍贯', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                college: [{ required: true, message: '请选择院系', trigger: 'change' }],
                nation: [{ required: true, message: '请输入民族', trigger: 'blur' }]
            },
            collegeList: [],
            select: ''
        };
    },
    methods: {
        registerForm() {
            if (this.param.idCard.length < 18) {
                this.$message.error('身份证位数未满18位，请重新输入');
                return false;
            }
            this.$refs.reg.validate(async valid => {
                if (valid) {
                    const res = await this.postJSON('/student/add', this.param);
                    if (res.code == 200) {
                        this.$message.success('注册成功');
                        this.$router.push('/login');
                    } else {
                        this.$message.error(res.data.msg);
                        return false;
                    }
                } else {
                    this.$message.error('信息输入有误');
                    return false;
                }
            });
        },
        // 获取所有的院系
        async allCollege() {
            const res = await this.getJSON(`/college/findall`);
            if (res.code == 200) {
                this.collegeList = res.data.records;
            } else {
                this.$message.error('院系数据加载失败');
            }
        }
    },
    created() {
        this.allCollege();
    }
};
</script>
<style scope>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
